package com.coman404.base;

import com.coman404.base.handler.UDPServerHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;

import java.util.Objects;

/**
 * @author NotFound404 at 2024/5/31
 * code code always code
 */
public class UDPServer {

    public static void main(String[] args) {
        int port = 8089;
        EventLoopGroup group = new NioEventLoopGroup();
        try{
            Bootstrap server = new Bootstrap();
            server.group(group)
                    .channel(NioDatagramChannel.class)
                    .option(ChannelOption.SO_BROADCAST,true)
                    .handler(new ChannelInitializer<NioDatagramChannel>() {
                        @Override
                        protected void initChannel(NioDatagramChannel ch) throws Exception {
                            ch.pipeline().addLast(new UDPServerHandler());
                        }
                    });
            ChannelFuture f = server.bind(port).sync();
            System.out.println("服务启动");
            f.channel().closeFuture().sync();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            group.shutdownGracefully();
        }
    }

}
